package com.hoppinzq.test;

import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;

import java.util.Set;

/**
 * @author ZhangQi
执行javaScript代码
 */
public class demo3 {

    public static void main(String[] args) {
        WebDriver driver = EdgeBrowser.getDriver();
        // 进到正确的域
        driver.get(EdgeBrowser.hoppinzqVideo);

        // 设置 cookie，这个cookie 对整个域都有效
        Cookie cookie = new Cookie("key", "value");
        driver.manage().addCookie(cookie);

        // 输出当前 url 所有可用的 cookie
        Set<Cookie> allCookies = driver.manage().getCookies();
        for (Cookie loadedCookie : allCookies) {
            System.out.println(String.format("%s -> %s", loadedCookie.getName(), loadedCookie.getValue()));
        }

        // 你可以通过3中方式删除 cookie
        // By name
        driver.manage().deleteCookieNamed("CookieName");
        // By Cookie
        //driver.manage().deleteCookie(loadedCookie);
        // Or all of them
        driver.manage().deleteAllCookies();

    }

    //public static String[] getAllCheckboxIds () {
//        String script = "var inputId  = new Array();";// Create array in java script.
//        script += "var cnt = 0;"; // Counter for check box ids.
//        script += "var inputFields  = new Array();"; // Create array in java script.
//        script += "inputFields = window.document.getElementsByTagName('input');"; // Collect input elements.
//        script += "for(var i=0; i<inputFields.length; i++) {"; // Loop through the collected elements.
//        script += "if(inputFields[i].id !=null " +
//                "&& inputFields[i].id !='undefined' " +
//                "&& inputFields[i].getAttribute('type') == 'checkbox') {"; // If input field is of type check box and input id is not null.
//        script += "inputId[cnt]=inputFields[i].id ;" + // Save check box id to inputId array.
//                "cnt++;" + // increment the counter.
//                "}" + // end of if.
//                "}"; // end of for.
//        script += "inputId.toString();" ;// Convert array in to string.
//        String[] checkboxIds = selenium.getEval(script).split(","); // Split the string.
//        return checkboxIds;
    //}
}

